package com.alog.yzd.jdbc.hm;

import com.alog.yzd.jdbc.User;

import java.sql.*;

public class UserDaoJdbcImpl implements UserDao {
    @Override
    public int addUser(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int i = -1;
        try{
           String sql = "insert into user (id, name, pwd, birth) values (?, ?, ?, ?)";
           conn = JdbcUtils2.getConnction();
           ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
           ps.setInt(1,user.getId());
           ps.setString(2,user.getName());
           ps.setString(3,user.getPwd());
           ps.setDate(4, new java.sql.Date(user.getBirth().getTime()));
           i = ps.executeUpdate();

           rs = ps.getGeneratedKeys();
           if(rs.next()){
               user.setId(rs.getInt(1));
           }
            return i;
        } catch(SQLException e){
            throw new DaoException(e.getMessage(), e);
        } finally {
            JdbcUtils2.release(rs, ps, conn);
        }
    }

    @Override
    public User getUser(int userId) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        try{
            String sql = "select id, name, pwd, birth from user where id = ?";
            conn = JdbcUtils2.getConnction();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,userId);
            rs = ps.executeQuery();
            if(rs.next()){
                user = mappingUser(rs);
            }
        } catch(SQLException e){
            throw new DaoException(e.getMessage(), e);
        } finally {
            JdbcUtils2.release(rs, ps, conn);
        }
        return user;
    }

    @Override
    public int updateUser(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            String sql = "update user set name = ?, pwd = ?, birth = ?  where id = ?";
            conn = JdbcUtils2.getConnction();
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getName());
            ps.setString(2,user.getPwd());
            ps.setDate(3,new java.sql.Date(user.getBirth().getTime()));
            ps.setInt(4,user.getId());
            int i = ps.executeUpdate();
            return i;
        } catch(SQLException e){
            throw new DaoException(e.getMessage(), e);
        } finally {
            JdbcUtils2.release(rs, ps, conn);
        }
    }

    @Override
    public int deleteUser(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            String sql = "delete from user where id < ?";
            conn = JdbcUtils2.getConnction();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,user.getId());
            int i = ps.executeUpdate();
            return i;
        } catch(SQLException e){
            throw new DaoException(e.getMessage(), e);
        } finally {
            JdbcUtils2.release(rs, ps, conn);
        }
    }

    @Override
    public User findUser(String name, String pwd) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        try{
            String sql = "select id, name, pwd, birth from user where name = ? and pwd = ?";
            conn = JdbcUtils2.getConnction();
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,pwd);
            rs = ps.executeQuery();
            if(rs.next()){
                user = mappingUser(rs);
            }
        } catch(SQLException e){
            throw new DaoException(e.getMessage(), e);
        } finally {
            JdbcUtils2.release(rs, ps, conn);
        }
        return user;
    }

    private User mappingUser(ResultSet rs) throws SQLException {
        User user = new User();
        user.setId(rs.getInt("id"));
        user.setName(rs.getString("name"));
        user.setPwd(rs.getString("pwd"));
        user.setBirth(rs.getDate("birth"));
        return user;
    }
}
